<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :page.sync="page"
      v-model="form"
      :option="option"
      :table-loading="loading"
      :data="data"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" :loading="exportBtnLoading">导出 </el-button>
      </template>
    </avue-crud>
    <export-comp
      ref="exportComp"
      page-api-url="mes-abacus/statProductionDay/page-query-work-order-efficiency"
      :option="option"
      :page="page"
      :query="query"
      file-name="工单效率查询"
    >
    </export-comp>
  </basic-container>
</template>

<script>
import { findProductionlineList } from '@/api/product/workorder';
import { exportWorkorderEfficiencyData, getWorkorderEfficiencyData } from '@/api/report/report';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp/index';

export default {
  name: 'workorderEfficiency',
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'WORKORDER_EFFICIENCY',
      option: {
        height: 'calc(100vh - 500px)',
        border: true,
        index: true,
        indexLabel: '序号',
        menu: false,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        columnBtn: false,
        refreshBtn: false,
        selection: true,
        tip: false,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        searchLabelWidth: 100,
        column: [
          {
            label: '工单时间',
            prop: 'datetimerange',
            type: 'datetimerange',
            width: 130,
            search: true,
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '计划开始时间',
            endPlaceholder: '计划结束时间',
            hide: true,
            order: 1,
            searchRequire: true,
            addDisplay: false,
            viewDisplay: false,
            editDisplay: false,
            searchRules: [
              {
                required: true,
                trigger: 'blur',
                message: '请选择工单时间'
              }
            ]
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            width: 150,
            order: 2,
            disabled: true,
            search: true,
            rules: [
              {
                required: true,
                message: '请输入生产工单',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '派工产线',
            prop: 'lineName'
          },
          {
            label: '产品名称',
            prop: 'partName',
            search: true,
            order: 3,
            placeholder: '请输入产品名称'
          },
          {
            label: '产品编码',
            prop: 'partNo',
            order: 4,
            search: true,
            placeholder: '请输入产品编码'
          },
          {
            label: '产品型号',
            prop: 'partModel'
          },
          {
            label: '工单直通率(%)',
            prop: 'firstPassYield'
          },
          {
            label: '工单完工率(%)',
            prop: 'completedRate'
          },
          {
            label: '计划开始时间',
            prop: 'planStartDate'
          },
          {
            label: '计划结束时间',
            prop: 'planEndDate'
          },
          {
            label: '工单计划数量',
            prop: 'planQuantity'
          },
          {
            label: '工单完工数量',
            prop: 'completedQuantity'
          },
          {
            label: '派工产线',
            prop: 'lineId',
            search: true,
            searchMultiple: false,
            type: 'select',
            order: 5,
            hide: true,
            placeholder: '请选择派工产线',
            dicData: []
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            order: 6,
            hide: true,
            search: true,
            width: 60,
            // slot: true,
            html: true,
            formatter: (val) => this.$statusText.GD(val.status),
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order'
          },
          {
            label: '状态',
            prop: 'statusFormat'
          }
        ]
      },
      exportBtnLoading: false,
      loading: false,
      form: {},
      data: [],
      query: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      }
    };
  },
  created() {
    this.getProductionLineList();
  },
  mounted() {
    this.$nextTick(() => {
      //搜索与表格结构分隔
      this.$structuralSplit();
    });
  },
  methods: {
    async onLoad(page, params = {}) {
      this.loading = true;
      try {
        const res = await getWorkorderEfficiencyData(page.currentPage, page.pageSize, Object.assign(params, this.query));
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      } catch (e) {
        console.warn(e);
      } finally {
        this.loading = false;
      }
    },
    searchReset() {
      this.query = {};
      this.data = [];
      this.page.total = 0;
    },
    searchChange(params, done) {
      if (params.datetimerange) {
        params.startTime = params.datetimerange[0];
        params.endTime = params.datetimerange[1];
        delete params.datetimerange;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    getProductionLineList() {
      findProductionlineList().then((res) => {
        const column = this.findObject(this.option.column, 'lineId');
        column.dicData = this._map(this._get(res, 'data.data', []), (item) => {
          return { label: item.name, value: item.id };
        });
        this.productionlineList = this._get(res, 'data.data', []);
      });
    }
  }
};
</script>

<style scoped></style>
